import { makeObservable, observable, computed, action, flow, runInAction, has } from "mobx"
import { useHistory } from 'react-router';
import dahai from 'common_library/dahai';
export const StageStatusNone = 0b00000000;
export const StageStatusCurrent = 0b00000001;
export const StageStatusFirstChild = 0b00000010;
const project = dahai.createProject('decision', 'target_plan');

export default class CardStageModel {
    @observable currentCard;
    @observable cardData;
    @observable isInit;
    @observable stageStatus = StageStatusNone;
    rootCardId = 'root';
    projectId;
    projectSdk;
    currentCardId;
    cardParents = [];
    constructor(routePath: string[]) {
        this.currentCardId = routePath[routePath.length - 1];
        this.projectId = routePath[0];
        this.projectSdk = project;
        makeObservable(this);
    }
    async changeCurrentCardId(cardId: string) {
        this.currentCardId = cardId;
        this.currentCard = await this.getCardData(cardId);
    }
    async initCardData() {
        const currentCardId = this.currentCardId;
        this.projectSdk.getFileContent(this.projectId).then(async data => {
            this.cardData = data || [];
            // 如果root 数据不存在, 那么就创建
            if (!currentCardId) {
                var newData = await this.createNewCard('根节点', true);
                this.currentCardId = newData.id;
                runInAction(() => {
                    this.currentCard = newData;
                });
            } else {
                var cardData = await this.getCardData(currentCardId);
                // 如果不存在, 就创建
                if (cardData == null) {
                    cardData = await this.createNewCard('根节点', true);
                }
                runInAction(async () => {
                    this.currentCard = cardData;
                });
            }
            this.isInit = true;
        });
    }
    createId() {
        return new Date().getTime()
    }
    async setCardData(id, data) {
        let isExist = false;
        for (let i = 0; i < this.cardData.length; i++) {
            if (this.cardData && this.cardData[i].id == id) {
                isExist = true;
                break;
            }
        }
        if (!isExist) {
            this.cardData.push(data);
        }
        
        this.currentCard = await this.getCardData(this.currentCardId);
        await this.projectSdk.updateFileContent(this.projectId, this.cardData);
    }
    getCurrentId() {
        const pathname = location.hash;
        var paths = pathname.split('/');
        if (paths.length >= 3) {
            const currentCardId = pathname.slice(pathname.lastIndexOf('/') + 1);
            return currentCardId;
        }
        return null;
    }
    async getCardData(id) {
        const data = this.cardData;
        const result = data.find(item => {
            return item.id == id;
        });
        return result ? result : null;
    }
    async delCardData(id) {
        for (let i = 0; i < this.cardData.length; i++) {
            if (this.cardData && this.cardData[i].id == id) {
                this.cardData.splice(i, 1);
                break;
            }
        }
        const result = await this.projectSdk.delFile(id);
        return result;
    }
    async saveCurrentCard() {
        await this.setCardData(this.currentCard.id, this.currentCard);
    }
    async createNewCard(title, isInit?: boolean) {
        if (isInit) {
            var id = this.rootCardId;
        } else {
            this.currentCard.idIndex++;
            var id = this.currentCard.id + '_' + (this.currentCard.idIndex || 1);
        }
        var data = {
            title: title,
            id: id,
            idIndex: 1,
            parentId: null,
            cols: [{
                width: '100',
                cards: []
            }],
        }
        await this.setCardData(data.id, data);
        return data;
    }
    // get double() {
    //     return this.value * 2
    // }

    // increment() {
    //     this.value++
    // }

    // *fetch() {
    //     const response = yield fetch("/api/value")
    //     this.value = response.json()
    // }
}